<?php
/**
 * File: UserInfo.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-19 19:54
 */

namespace App\Models;


class UserInfo extends BaseModel
{
    public $table = 'user_info';
    public $primary = 'uid';
    public $timestamps = true;

    public $uid;
    public $user_name;
    public $user_avatar;
    public $user_open_id;
    public $phone;
    public $user_idno;
    public $user_role;
    public $vote_price;
    public $student_no;
    public static $UserRoles = [
        'default' => '默认用户(1)',
        'normal' => '普通用户(2)',
        'admin' => '特权用户',
        'vote_5' => '特殊用户(5)'
    ];


    public function openidExists($openid)
    {
        return $this->has(['user_open_id' => $openid, 'state' => 1]);
    }

    public function findUserByNo_ID($studentNo = null, $idno = null)
    {
        $condition = [];
        if ($studentNo) {
            $condition['student_no'] = $studentNo;
        }
        if ($idno) {
            $condition['user_idno'] = $idno;
        }
        if (empty($condition)) {
            return null;
        }
        $condition['state'] = 1;
        return $this->get('*', $condition);
    }


    public function findObjectByStudentOrIdno($studentNo = null, $idno = null)
    {
        $data = $this->findUserByNo_ID($studentNo, $idno);
        return $data ? $this->setProperty($data) : null;
    }

    public function findObject($condition)
    {
        $data = $this->get('*', $condition);
        return $data ? $this->setProperty($data) : null;
    }
}